//
//  MHUserInfoViewController.m
//  MHO
//
//  Created by papo on 2/24/14.
//  Copyright (c) 2014 maihoang. All rights reserved.
//

#import "MHRegisterViewController.h"
#import "MHAppDelegate.h"
#import "MHNavibarView.h"
#import "Lib.h"
#import "Contants.h"
#import "Account.h"
#import "MHPoliciesViewController.h"

@interface MHRegisterViewController ()
{
    MHNavibarView* navibarView;
//    __weak IBOutlet UIWebView *webView;
}
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@end

@implementation MHRegisterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    navibarView = [MHNavibarView createWithTitle:@"Đăng ký" leftButton:nil rightButton:nil searchButton:NO];
    [navibarView setActionSelectorDelegate:self left:@selector(leftButtonPressed:) right:nil search:nil];
    [navibarView setRootViewController:self];
    [self.view addSubview:navibarView];
    
//    [webView loadHTMLString:@"Khi bạn nhấn vào \"Đăng ký\" tức là bạn đã đọc và đồng ý với <a href=http://www.google.com.vn>điều khoản sử dụng</a> của Mai Hoàng Online." baseURL:Nil];
    
    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    [self.scrollView addGestureRecognizer:tap];
    
    self.scrollView.contentSize = self.scrollView.frame.size;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)tapGesture:(UIGestureRecognizer*)gesture {
    [self.scrollView endEditing:YES];
}

#pragma mark - TextField Delegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    float offset = 216;
    CGRect frame = self.scrollView.frame;
    if (self.view.frame.size.height - frame.size.height - frame.origin.y < offset) {
        frame.size.height -= offset;
    }
    self.scrollView.frame = frame;
    CGPoint curOffset = self.scrollView.contentOffset;
    CGRect tFrame = textField.frame;
    if (tFrame.origin.y - curOffset.y < 0) {
        [self.scrollView setContentOffset:CGPointMake(0, textField.frame.origin.y) animated:YES];
    }
    else if (tFrame.origin.y + tFrame.size.height - curOffset.y > frame.size.height) {
        [self.scrollView setContentOffset:CGPointMake(0, tFrame.origin.y - frame.size.height + tFrame.size.height) animated:YES];
    }
//    [self.scrollView setContentOffset:CGPointMake(0, textField.frame.origin.y - 100) animated:YES];
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    float offset = 216;
    CGRect frame = self.scrollView.frame;
    if (self.view.frame.size.height - frame.size.height - frame.origin.y >= offset) {
        frame.size.height += offset;
    }
    self.scrollView.frame = frame;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}


- (void)leftButtonPressed:(id)sender {
    [self.scrollView endEditing:YES];
    [[MHAppDelegate sharedApp].viewController showLeftPanelAnimated:YES];
}
- (IBAction)registerButtonPressed:(id)sender {
    NSString* email = self.email.text;
    NSString* pass = self.password.text;
    NSString* repass = self.repasword.text;
    NSString* phone = self.phoneNumber.text;
    NSString* refEmail = self.referEmail.text;
    if (strCheckNull(email).length <= 0) {
        [Lib showMessage:@"Bạn chưa nhập email!" title:@""];
    }else
    if (strCheckNull(pass).length <= 0) {
        [Lib showMessage:@"Bạn chưa nhập password!" title:@""];
    }else
    if (![strCheckNull(pass) isEqualToString:strCheckNull(repass)]) {
        [Lib showMessage:@"Password không trùng nhau!" title:@""];
    }else
    if (strCheckNull(phone).length <= 0) {
        [Lib showMessage:@"Bạn chưa nhập số điện thoại!" title:@""];
    }
    
    else {
        refEmail = strCheckNull(refEmail);
        if (refEmail.length==0) {
            refEmail = @"null";
        }
//        BOOL result = NO;
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            ServiceResult* result = [Account registerUserWithDictionary:@{kAccountEmail: email, kAccountPassword: pass, @"phone": phone, @"pemail": refEmail}];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                if (result.success) {
                    [Lib showMessage:@"Đăng nhập thành công!" title:@""];
                    [[MHAppDelegate sharedApp].viewController updateUserInfo];
                }
                else {
                    [Lib showMessage:result.message title:@""];
                }
            });
        });
        
        
    }
//    [Account re]
}
- (IBAction)openDieuKhoanUrl:(id)sender {
    MHPoliciesViewController* vc = [[MHPoliciesViewController alloc] initWithNibName:@"MHPoliciesViewController" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
